#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Set_up.py
@Time    :   2022/04/13 18:00:00
@Author  :   WangWei 
@Version :   1.0
@Contact :   wangwei@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''
# here put the import lib
import datetime
import  os
from xml.dom.expatbuilder import FilterVisibilityController

# HW config
#CAN
drive_type = 'USE_TS_TC1014_DEVICE'   # USE_TS_TC1011_DEVICE /   USE_TS_VIRTUAL_DEVICE
CAN_Type = 'CANFD'
if CAN_Type == 'CAN':
    configure_baudrate = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
else:
    configure_baudrate = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}

CAN_Type2 = 'CANFD'
if CAN_Type2 == 'CAN':
    configure_baudrate2 = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
else:
    configure_baudrate2 = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}

# CAN_Type3 = 'CANFD'
# if CAN_Type3 == 'CAN':
#     configure_baudrate3 = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
# else:
#     configure_baudrate3 = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}

#Dbc define
root = os.getcwd()
project_DBC_File_CAN = os.path.join(root, r'data\CMRR_RR_CANFD_V1.11_20240913.dbc') # RR
pubpem = os.path.join(root, r'data\pub_dev.pem') # RR
blfname = '.blf'
project_BLF_File_CAN = os.path.join(root, f"blf\{blfname}")

NetName = 'BJEV_BE12A'
nodeName= 'GW'
# EnableNode = ['CGW_Safety','EPS_Safety','APA_Safety','ABS_ESP_Safety','FVC_Safety']
# EnableNode = ['CGW','ADAS']
EnableNode = ['GW']

#Dbc define
source='power'
baudrate=9600

#test radar_type
# act_radar_type = "CMRR_FR"
act_radar_type = "RRCR"
# act_radar_type = "LRCR"


#Dbc UD

# CMRR_RL
# reqID = 0x74C
# resID = 0x7CC
# AFctID = 0x7DF



vehicle_type_key_Tab ={ 'LRCR':{'req_ID':0x749,'res_ID':0x7C9},    #3 都悬空  左后
                        'RRCR':{'req_ID':0x74C,'res_ID':0x7CC},    #4 IGN接地 右后
                        }

reqID = vehicle_type_key_Tab[act_radar_type]['req_ID']
resID = vehicle_type_key_Tab[act_radar_type]['res_ID']
AFctID = 0x7DF

NM_modbusCOM = 'COM8'

# '''选择刷写文件'''
FlashDrivepath = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' + 'FlashDrv.hex'
Bootpath = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' + '001_Boot_006.hex'
Apppath = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' + '002_App_S010003_3.1.0.13.hex'
old_Bootpath = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' + 'old_boot.hex'
old_Apppath = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' + 'old_app.hex'
ErrorApppath = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' + 'firmware_error.hex'




#BJEV_To_ BE12
# FlashDrivepath = os.path.abspath(os.path.join(os.getcwd(),"./BJEV_To_ BE12")) + '\\' + 'FlashDrv.hex'
# Bootpath = os.path.abspath(os.path.join(os.getcwd(),"./BJEV_To_ BE12")) + '\\' + '001_Boot_006.hex'
# Apppath = os.path.abspath(os.path.join(os.getcwd(),"./BJEV_To_ BE12")) + '\\' + '002_App_S010003_3.1.0.12.hex'
# old_Bootpath = os.path.abspath(os.path.join(os.getcwd(),"./BJEV_To_ BE12")) + '\\' + 'old_boot.hex'
# old_Apppath = os.path.abspath(os.path.join(os.getcwd(),"./BJEV_To_ BE12")) + '\\' + 'old_app.hex'
# ErrorApppath = os.path.abspath(os.path.join(os.getcwd(),"./BJEV_To_ BE12")) + '\\' + 'firmware_error.hex'


# 0x22 DID
#Bootloader软件版本号 F183
boottime = [0x20,0x24,0x10,0x09]  # 每版本需要改一下
bootver = [0x30,0x30,0x36]  #  每版本需要改一下
boottime1 = '20 24 10 09'  # F199 每版本需要改一下
bootver1 = '30 30 36' #  每版本需要改一下
internalver_boot1 = '03 01 01 33 23 04 13 05'
internalver_boot2 = '03 01 01 fD 24 10 09 fD' # FE03

calVer = '00 00 08 08 23 04 13 00' #FE04

if act_radar_type == "RRCR":
    BootloaderSoftwareVersionNumber = [0x43,0x52,0x52] + boottime + bootver # 0xF183
    BootloaderSoftwareVersionNumber1 = '43 52 52' + boottime1 + bootver1
if act_radar_type == "LRCR":
    BootloaderSoftwareVersionNumber = [0x43,0x52,0x4C] + boottime + bootver
    BootloaderSoftwareVersionNumber1 = '43 52 4C' + boottime1 + bootver1
if act_radar_type == "LFCR":
    BootloaderSoftwareVersionNumber = [0x43,0x46,0x4C] + boottime + bootver
    BootloaderSoftwareVersionNumber1 = '43 46 4C' + boottime1 + bootver1
if act_radar_type == "RFCR":
    BootloaderSoftwareVersionNumber = [0x43,0x46,0x52] + boottime + bootver
    BootloaderSoftwareVersionNumber1 = '43 46 52' + boottime1 + bootver1

# 应用软件指纹信息 F184
ApplicationSoftwareFingerprint = boottime[1:4] + [0x02,0x02,0x02,0x02,0x02,0x02]
ApplicationSoftwareFingerprint1 = boottime1[3:] + '02 02 02 02 02 02'

# 编程日期 F199
ProgrammingDate = boottime   # 时间读取的是F184里面的  每版本需要改一下
ProgrammingDate1 = boottime1

# 软件版本号 F195
ECUSoftwareversionnumber = [0x20,0x53,0x30,0x31,0x30,0x2e,0x30,0x30,0x33] # S010.003
ECUSoftwareversionnumber1 = '20 53 30 31 30 2e 30 30 33' # S010.003
internalver1 = '03 01 00 0D 25 01 02 03'  # 3.1.0.13 内部app版本号 FE01

# 功能寻址 NRC 11 12 31 7E 7F不响应




Radarangle = 0x2D # 0x3F1A

# ECU PartNumberAlphaCode  F187
PartNumber = [0x41,0x30,0x30,0x31,0x33,0x33,0x30,0x30,0x39]   # 零部件号  A00133009, B60VS:A00133009
PartNumber1 = '41 30 30 31 33 33 30 30 39'
# ECU SupplierCode F18A
SupplierCode = [0x44,0x34,0x33,0x31,0x45,0x30,0x31,0x36,0x31,0x38]   # 供应商代码 D431E01618
SupplierCode1 = '44 34 33 31 45 30 31 36 31 38'

# ECU HardwareVersionNumber  F191
HardwareVersionNumber = [0x20,0x20,0x20,0x20,0x20,0x48,0x30,0x30,0x31]  # ECU 硬件版本号 H001
HardwareVersionNumber1 = '20 20 20 20 20 48 30 30 31'

# 初始存在故障
InitialFailure = [[0xD7,0x62,0x87,0x09],[0x98,0x7C,0x54,0x09],[0xF2,0x4F,0x88,0x09],[0xF2,0x4F,0x88,0x08]]

DTC_list = [    ([0x98,0x8C,0x98],'System_over_temperature_fault'),
                ([0x98,0x7A,0x17],'DTC_VBAT_HIGH'),			
                ([0x98,0x7A,0x16],'DTC_VBAT_LOW'),
                ([0xF2,0x4C,0x88],'DTC_SFCAN_BUSOFF'),		
				([0xF2,0x4F,0x88],'DTC_PCAN_BUSOFF'),
                ([0x98,0x96,0x78],'Excessive_installation_angle_error'),			
				([0x98,0x7C,0x54],'DTC_ALIGNMENT_NEVER_DONE'),
                ([0x98,0x99,0xFF],'Internal_electronic_failure'),
                ([0x98,0x8C,0x00],'System_occlusion_fault'),               
                ([0x98,0x8C,0x78],'Address_line_error_unstable'),
                ([0x98,0x8C,0x96],'HW_Failure'),
                ([0xD7,0x62,0x87],'Missing_opposite_radar'),
                ([0xF2,0x43,0x87],'lost_communication_with_BCM'),
                ([0xF2,0x43,0x83],'Checksum_BCM_error'),
                ([0xF2,0x43,0x82],'Rollingcounter_BCM_error'),
                ([0xF2,0x40,0x87],'lost_communication_with_PBM'),
                ([0xF2,0x40,0x81],'Invalid_vehiclel_speed_reported_by_PBM'),
                ([0xF2,0x41,0x81],'Rctb_not_available'),
                ([0xF2,0x40,0x83],'Checksum_PBM_error'),
                ([0xF2,0x40,0x82],'Rollingcounter_PBM_error'),
                ([0xD7,0x6F,0x87],'lost_communication_with_VCU'),
                ([0xD7,0x6F,0x81],'Invalid_gear_position_reported_by_VCU'),
                ([0xD7,0x6F,0x83],'Checksum_VCU_error'),
                ([0xD7,0x6F,0x82],'Rollingcounter_VCU_error'),
                ([0xD7,0x64,0x87],'lost_communication_with_YRS'),
                ([0xD7,0x64,0x81],'Invalid_vehicle_dynamic_LateralAcce_value_reported_by_YRS'),
                ([0xD7,0x65,0x81],'Invalid_vehicle_dynamic_LongitudeAcc_value_reported_by_YRS'),
                ([0xD7,0x66,0x81],'Invalid_vehicle_dynamic_yawrate_value_reported_by_YRS'),
                ([0xD7,0x64,0x83],'Checksum_YRS_error'),
                ([0xD7,0x64,0x82],'Rollingcounter_YRS_error'),
                ([0xF2,0x48,0x87],'lost_communication_with_PEPS'),
                ([0xF2,0x48,0x81],'invalid_power_mode_by_PEPS'),
                ([0xF2,0x48,0x83],'Checksum_PEPS_error'),
                ([0xF2,0x48,0x82],'Rollingcounter_PEPS_error'),
                ([0xD7,0x6A,0x87],'lost_communication_with_SAS'),
                ([0xD7,0x6A,0x81],'Invalid_angle_of_steering_wheel_value_reported_by_EPS'),
                ([0xD7,0x6A,0x83],'Checksum_EPS_error'),
                ([0xD7,0x6A,0x82],'Rollingcounter_EPS_error'),
            ]

DTC_list_all = {'DTC_VBAT_HIGH':                        [0x98,0x7A,0x17],			
                'DTC_VBAT_LOW':                         [0x98,0x7A,0x16],
                'Over Temperature':                     [0x98,0x8C,0x98],
                'DTC_SFCAN_BUSOFF':                     [0xF2,0x4C,0x88],		
				'DTC_PCAN_BUSOFF':                      [0xF2,0x4F,0x88],
                'Excessive_installation_angle_error':   [0x98,0x96,0x78],			
				'DTC_ALIGNMENT_NEVER_DONE':             [0x98,0x7C,0x54],
                'Internal_electronic_failure':          [0x98,0x99,0xFF],
                'System_occlusion_fault':               [0x98,0x8C,0x00],
                'Address_line_error_unstable':          [0x98,0x8C,0x78],
                'HW_Failure':                           [0x98,0x8C,0x96],
                'Missing_opposite_radar':               [0xD7,0x62,0x87],
                'lost_communication_with_BCM':          [0xF2,0x43,0x87],
                'Checksum_BCM error':                   [0xF2,0x43,0x83],
                'Rollingcounter_BCM error':             [0xF2,0x43,0x82],
                'lost_communication_with_ESP':          [0xF2,0x40,0x87],
                'Invalid_vehiclel_speed_reported_by_ESP': [0xF2,0x40,0x81],
                'Rctb_not_available':                   [0xF2,0x41,0x81],
                'Checksum_ESP error':                   [0xF2,0x40,0x83],
                'Rollingcounter_ESP error':             [0xF2,0x40,0x82],
                'lost_communication_with_EMS':          [0xD7,0x6F,0x87],
                'Invalid_gear_position_reported_by_EMS': [0xD7,0x6F,0x81],
                'Checksum_EMS error':                   [0xD7,0x6F,0x83],
                'Rollingcounter_EMS error':             [0xD7,0x6F,0x82],
                'lost_communication_with_YRS':          [0xD7,0x64,0x87],
                'Invalid_vehicle_dynamic_LateralAcce_value_reported_by_YRS': [0xD7,0x64,0x81],
                'Invalid_vehicle_dynamic_LongitudeAcc_value_reported_by_YRS': [0xD7,0x65,0x81],
                'Invalid_vehicle_dynamic_yawrate_value_reported_by_YRS': [0xD7,0x66,0x81],
                'Checksum_YRS error':                   [0xD7,0x64,0x83],
                'Rollingcounter_YRS error':             [0xD7,0x64,0x82],
                'lost_communication_with_PEPS':         [0xF2,0x48,0x87],
                'invalid_power_mode_by_PEPS':           [0xF2,0x48,0x81],
                'Checksum_PEPS error':                  [0xF2,0x48,0x83],
                'Rollingcounter_PEPS error':            [0xF2,0x48,0x82],
                'lost_communication_with_SAS':          [0xD7,0x6A,0x87],
                'Invalid_angle_of_steering_wheel_value_reported_by_EPS': [0xD7,0x6A,0x81],
                'Checksum_EPS error':                   [0xD7,0x6A,0x83],
                'Rollingcounter_EPS error':             [0xD7,0x6A,0x82],
            }